#include "odn_file_Reader.h"

#include <cstring>

using std::string;
using std::cout;
using std::endl;

namespace ouden
{
	namespace file
	{
		
		//Load all data from the specified file 
		bool ODN_LoadableObject::loadFromDataFile(char* Filename)
		{
			
			assert(Filename);
			
			//Open file for reading binary data 
			file = fopen(Filename, "rb");
			
			//Check that the file loaded ok 
			if (!file) {
				
				cout << "File " << Filename << " was not loaded, possibly does not exist" << endl;
				return false;
				
			}
			
			//Loop through the file 
			while (!feof(file)) {
				
				//Create temporary variable to hold current section header 
				sSectionHeader SectionHeader;
				
				//Load the section header 
				fread(&SectionHeader, sizeof(SectionHeader), 1, file);
				
				//Store the current reading position in the file 
				long SectionBeginning = ftell(file);
				
				//Call the overloaded function to process the section
				processDataSection(SectionHeader);
				
				//Jump to the next section by skipping over this section
				//This means it doesn't matter whether processDataSection recognised the section or not 
				fseek(file, SectionBeginning + SectionHeader.SectionLength, SEEK_SET);
				
			}
			
			//TODO: Check if this should be here 
			//fclose(file);
			
		}
		
		void ODN_SaveableObject::beginSection(WORD SectionID)
		{
			
			if /* already */ (insidesection) {
			
			   	std::cout << "New section begun before old section ended. Possible missing endSection()" << std::endl;
			    return;
			
			}
		
		    //Let it know we're now inside a section
		    insidesection=true;
		
		   	//Reset the current section length variable
			cursectionlength = 0;
		
		    //Store the offset of the new section in the file
		    cursectionoffset = ftell(file);
			
		}
		
		void ODN_SaveableObject::endSection()
		{
			
			//Jump back to the position where the section length is to be stored (in the section header, just after the SectionID)
			fseek(cursectionoffset + sizeof(sHeader.SectionID), SEEK_CUR);
		
		   	//Write the length of the section into the section header
		   	fwrite(cursectionlength, sizeof(cursectionlength), 1, file);
		
		   	//Let it know we're no longer inside a section
		   	insidesection = false;
			
		}
		
		/*
		string ReadTag(string Data, string Name, int Start)
		{
			
			static int PreviousStart = 0;
			
			if (Start == -1)
				Start = PreviousStart;
			
			cout << Start << endl;
			
			//Find the starting and ending tags 
			int start = Data.find("<" + Name + ">", Start) + Name.length() + 2;
			int end = Data.find("</" + Name + ">", start);
			cout << start << endl;
			if (start == -1) {
				cout << "Could not find tag " << Name << endl;
				return "";
			}
			
			if (end == -1) {
				cout << "Could not find end of tag " << Name << endl;
				return "";
			}
			
			PreviousStart = end + Name.length() + 3;
			
			//Return the data between them 
			return Data.substr(start, end - start);
			
		}
		
		string GetLine(FILE* File)
		{
		
			char oneline[255];
		
		    //Make sure it's not a blank line or comment
		    //while (oneline[0] == '\n' || oneline[0] == '/'){
		
		        fgets(oneline, 255, File);
		    
		    //}
		
			return oneline;
		
		}
		
		string ReadFile(string Name)
		{
			
			//Open file for reading text
			FILE* File = fopen(Name.c_str(), "rt");
			
			if (!File) {
				
				cout << "No file " << endl;
				return "";
				
			}
			
			string Data = "";
			
			//fscanf(File, "%s", &Data);
			
			while (!feof(File)) {
				
				Data += GetLine(File);
				
			}
			
			fclose(File);
			
			return Data;
			
		}
		*/
	} //namespace file
} //namespace ouden
